/*
 * Copyright (c) 2014, 2015, XIANDIAN and/or its affiliates. All rights reserved.
 * XIANDIAN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xiandian.cloud.storage;

import com.xiandian.cloud.storage.fragment.TypeObjectListFragment;
import com.xiandian.cloud.storage.fragment.NavigationDrawerFragment;
import com.xiandian.cloud.storage.fragment.ObjectListFragment;
import com.xiandian.cloud.storage.fragment.RecycleObjectListFragment;
import android.app.Activity;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.support.v4.widget.DrawerLayout;

/**
 * 主界面Activity
 * @author 云计算应用与开发项目组
 * @since  V1.0
 */
public class ObjectListActivity extends Activity implements
		NavigationDrawerFragment.NavigationDrawerCallbacks {

	/**
	 * Fragment managing the behaviors, interactions and presentation of the
	 * navigation drawer.
	 */
	private NavigationDrawerFragment mNavigationDrawerFragment;

	/**
	 * Used to store the last screen title. For use in
	 * {@link #restoreActionBar()}.
	 */
	private CharSequence mTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_object_list);

		mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
				.findFragmentById(R.id.navigation_drawer);
		mTitle = getTitle();

		// Set up the drawer.
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
				(DrawerLayout) findViewById(R.id.drawer_layout));
		
		onSectionAttached(mNavigationDrawerFragment.mCurrentSelectedPosition);
	}

	@Override
	public void onNavigationDrawerItemSelected(int position) {
		// update the main content by replacing fragments
		FragmentManager fragmentManager = getFragmentManager();
		switch (position) {
		case 0:
			fragmentManager
			.beginTransaction()
			.replace(R.id.container,
					ObjectListFragment.getInstance()).commit();
			break;
		case 1: // 我的文档
			TypeObjectListFragment documentObjectListFragment=new TypeObjectListFragment(this,0);
			fragmentManager
			.beginTransaction()
			.replace(R.id.container,
					documentObjectListFragment).commit();
			break;
		case 2: // 我的图片
			TypeObjectListFragment imageObjectListFragment=new TypeObjectListFragment(this,1);
			fragmentManager
			.beginTransaction()
			.replace(R.id.container,
					imageObjectListFragment).commit();
			break;
		case 3: // 我的视频
			TypeObjectListFragment videoObjectListFragment=new TypeObjectListFragment(this,2);
			fragmentManager
			.beginTransaction()
			.replace(R.id.container,
					videoObjectListFragment).commit();
			break;
		case 4: // 回收站
			RecycleObjectListFragment recycleObjectListFragment=new RecycleObjectListFragment(this);
			fragmentManager
			.beginTransaction()
			.replace(R.id.container,recycleObjectListFragment).commit();
			break;
		default:
			break;
		}
		
		onSectionAttached(position);
	}

	public void onSectionAttached(int number) {
		switch (number) {
		case 0:
			mTitle = getString(R.string.title_section1);
			break;
		case 1:
			mTitle = getString(R.string.title_section2);
			break;
		case 2:
			mTitle = getString(R.string.title_section3);
			break;
		case 3:
			mTitle = getString(R.string.title_section4);
			break;
		case 4:
			mTitle = getString(R.string.title_section5);
			break;
		}
	}

	public void restoreActionBar() {
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mNavigationDrawerFragment.isDrawerOpen()) {
			// Only show items in the action bar relevant to this screen
			// if the drawer is not showing. Otherwise, let the drawer
			// decide what to show in the action bar.
			//getMenuInflater().inflate(R.menu.object_list, menu);
			restoreActionBar();
			return true;
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onBackPressed() {
		switch (mNavigationDrawerFragment.mCurrentSelectedPosition) {
		case 0:
			ObjectListFragment.getInstance().onBackPressed();
			break;

		default:
			super.onBackPressed();
			break;
		}
		
	}
	
	
}
